package com.gliese.renren;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
import com.gargoylesoftware.htmlunit.html.HtmlForm;
import com.gargoylesoftware.htmlunit.html.HtmlImage;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlPasswordInput;
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
import com.gliese.renren.util.RenrenPropertiesUtil;
import com.gliese.renren.util.RenrenUtil;

/**
 * Hello world!
 * 
 */
public class RenrenApp {

	private static final Pattern pattern = Pattern.compile(".*&id=(\\d+)$");

	public static void main(String[] args) {
		final WebClient wc = new WebClient();
		try {
			List<HtmlAnchor> ans = findOnlineOrLivelyUserFriendProfiles(RenrenApp
					.loginToRenRen(wc));
			for (HtmlAnchor an : ans) {
				System.out.println(an.getHrefAttribute() + " : "
						+ an.getTextContent());
				HtmlPage friendProfile = enterFriendProfile(an);
				findOnlineOrLivelyUserFriendProfiles(friendProfile);
			}
			RenrenPropertiesUtil.storeHasViewProfileIds();
			RenrenPropertiesUtil.storeToViewProfileIds();
		} catch (Exception e) {
			e.printStackTrace();
		}
		wc.closeAllWindows();
		System.exit(0);

	}

	public static HtmlPage loginToRenRen(WebClient wc) throws Exception {

		final HtmlPage loginPage = wc.getPage("http://www.renren.com");
		List<HtmlForm> loginForms = loginPage.getDocumentElement()
				.getElementsByAttribute("form", "id", "loginForm");
		HtmlForm loginForm = loginForms.get(0);
		final HtmlTextInput emailField = loginForm.getInputByName("email");
		final HtmlPasswordInput passwordField = loginForm
				.getInputByName("password");
		// set username and passwrod , should be getten from resource file .
		emailField.setValueAttribute("");
		passwordField.setValueAttribute("");
		final HtmlSubmitInput submitButton = loginForm.getElementById("login");
		HtmlPage homePage = submitButton.click();
		List<HtmlAnchor> profileAnchors = homePage.getDocumentElement()
				.getElementsByAttribute("a", "stats", "Hd_Profile");
		HtmlPage profilePage = profileAnchors.get(0).click();
		return profilePage;
	}

	public static HtmlPage toFriendProfilePage(WebClient wc, String id) {
		String url = "http://www.renren.com/profile.do?id=" + id;
		try {
			
			RenrenPropertiesUtil.getHasViewIds().add(id);
			RenrenPropertiesUtil.getToViewIds().remove(id);
			return wc.getPage(url);

		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			System.out.println("");
		}
	}

	/*
	 * find the friends online on one's profile Page .
	 */

	public static List<HtmlAnchor> findOnlineOrLivelyUserFriendProfiles(
			HtmlPage profilePage) {
		// to add ids that required : 1, not in Set hasViewedIds,2,not in Set
		// hsId
		List<HtmlAnchor> onlineAnchors = new ArrayList<HtmlAnchor>();
		// ids in the anchors who has been added
		HashSet<String> hsIds = new HashSet<String>();
		// http://a.xnimg.cn/imgpro/icons/vip-online.gif
		String onlineImageSrc = "http://a.xnimg.cn/imgpro/icons/online_1.gif";
		List<HtmlImage> onlineImages = profilePage.getDocumentElement()
				.getElementsByAttribute("img", "src", onlineImageSrc);

		for (HtmlImage image : onlineImages) {
			HtmlAnchor onlineAnchor = (HtmlAnchor) image.getNextSibling();

			String profileId = RenrenUtil.getIdFromHtmlAnchor(pattern,
					onlineAnchor);
			if (!hsIds.contains(profileId)
					&& !RenrenPropertiesUtil.getHasViewIds()
							.contains(profileId)) {
				onlineAnchors.add(onlineAnchor);
				hsIds.add(profileId);

			}
		}
		// lively-user
		List<HtmlAnchor> livelyUsers = profilePage.getDocumentElement()
				.getElementsByAttribute("a", "class", "lively-user");
		for (HtmlAnchor livelyUser : livelyUsers) {
			String profileId = RenrenUtil.getIdFromHtmlAnchor(pattern,
					livelyUser);
			if (!hsIds.contains(profileId)
					&& !RenrenPropertiesUtil.getHasViewIds()
							.contains(profileId)) {
				onlineAnchors.add(livelyUser);
				hsIds.add(profileId);

			}
		}
		// add hsId to RenrenPropertiesUtil.toViewIds.
		RenrenPropertiesUtil.getToViewIds().addAll(hsIds);
		return onlineAnchors;
	}

	public static HtmlPage enterFriendProfile(HtmlAnchor onlineAnchor)
			throws IOException {
		HtmlPage friendProfile = onlineAnchor.click();
		String friendId = RenrenUtil.getIdFromHtmlAnchor(pattern, onlineAnchor);
		RenrenPropertiesUtil.getHasViewIds().add(friendId);
		RenrenPropertiesUtil.getToViewIds().remove(friendId);
		return friendProfile;
	}
}
